package com.csxy.yhj.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.csxy.yhj.entity.Caregiver;
import com.csxy.yhj.entity.vo.CaregiverQuery;
import com.csxy.yhj.service.CaregiverService;

import com.csxy.yhj.yhj.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "edu-teacher", description = "护工管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class CaregiverController {
    @Autowired
    private CaregiverService caregiverService;

    @Operation(summary = "查询所有护工")
    @GetMapping("findAll")
    public R getAllTeacher() {   //R：common-utils里的类，封装的返回结果类,统一返回值
        List<Caregiver> list = caregiverService.list();
        return R.ok().data("items", list);
    }

    //逻辑删除,删除成功返回true,表中数据还在，数据库中is_delete里面1表示被逻辑删除，查询不到
    @Operation(summary = "逻辑删除护工")
    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable("id") String id) {
        boolean result = caregiverService.removeById(id);
        if (!result){
            return R.error();
        }
        return R.ok();         //返回成功
    }

    //分页查询
    @Operation(summary = "分页查询护工")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageListCaregiver(@PathVariable("page") Long page,
                             @PathVariable("limit") Long limit) {
        Page<Caregiver> pagelist = caregiverService.pageListCaregiver(page, limit);
        return R.ok().data("total", pagelist.getRecords()).data("rows", pagelist.getRecords());
    }

    //新增护工
    @Operation(summary = "新增护工",description = "新增护工接口")
    @PostMapping("/save")
    public R save(@RequestBody Caregiver eduTeacher) {
        boolean flag = caregiverService.save(eduTeacher);
        if (flag){
            return R.ok();
        }
        return R.error();
    }

    //根据Id查询,用于信息回显
    @Operation(summary = "根据Id查询护工",description = "根据Id查询护工")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable("id") String id) {
        Caregiver teacher = caregiverService.getById(id);
        return R.ok().data("item", teacher);
    }

    //修改护工
    @Operation(summary = "修改护工",description = "修改护工")
    @PostMapping("/updateById")
    public R updateById(@RequestBody Caregiver teacher) {
        boolean flag = caregiverService.updateById(teacher);  //
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //多条件查询护工带分页
    @Operation(summary = "多条件查询护工带分页",description = "多条件查询护工带分页")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
        public R pageTeacherCondition(@Parameter(name = "page", description = "当前页码",required = true)
                                      @PathVariable Long page,

                                      @Parameter(name = "limit", description = "每页记录数",required = true)
                                      @PathVariable  Long limit,
                                      //通过@RequestBody将json数据转换成TeacherQuery对象
                                      @RequestBody(required = false) CaregiverQuery teacherQuery) {//通过封装TeacherQuery对象
        //创建分页page对象
        Page<Caregiver> pageParam = new Page<>(page, limit);
        //调用service方法实现多条件分页查询
        caregiverService.pageQuery(pageParam, teacherQuery);
        //获取查询到的数据
        List<Caregiver> records = pageParam.getRecords();
        //获取总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

}
